home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / gnu / unixcmds.lha / unixcmds / src / strings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-06  |  4.9 KB  |  165 lines

  1. /* strings - print ASCII strings in a file      Author: Peter S. Housel */
  2.  
  3. /*
  4.   This is a version of the BSD "strings" program for Minix. It is used
  5.   to search a file for printable strings. To install,
  6.  
  7.   cc -o strings strings.c
  8.   chmem =8000 strings
  9.  
  10. Command:  strings - search file for printable strings
  11. Syntax:   strings [-] [-o] [-len] file ...
  12. Flags:    -     Search the entire file. If this option is not given, only
  13.                 the initialized data segment of files that appear to be
  14.                 "a.out" format is searched.
  15.           -o    Print the offset (in octal) with each string.
  16.           -len  Use "len" as the minimum string length. The default is 4.
  17.  
  18. Examples: strings core
  19.           strings -o a.out > str
  20.  
  21. Strings searches the specified file(s) for printable ASCII strings (four
  22. or more printable characters followed by a newline or a null) and writes
  23. them to the standard output. This can be used to find out, for example, to
  24. find out what program a "core" file came from, what kinds of error messages
  25. are in an executable, or to see ASCII data hidden in a "binary" data file.
  26.  
  27. P.S. The program doesn't use the "a.out.h" file posted last week by
  28. Dick van Veen, both because it was written before then, and because
  29. not everybody has a.out.h yet. Future revisions probably ought to, though.
  30.  
  31. */
  32.  
  33.  
  34.  
  35. #include <ctype.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39.  
  40. /* Minix (8086 version) dependant definitions */
  41. #define SMALLMAGIC      0x04100301L     /* small model a.out magic number */
  42. #define SEPARATEMAGIC   0x04200301L     /* separate instruction/data a.out */
  43.  
  44. #define HDR_MAGIC       0       /* 0'th long  magic number */
  45. #define HDR_HSIZE       1       /* 1'st long  size of header */
  46. #define HDR_TSIZE       2       /* 2'nd long  size of text */
  47. #define HDR_DSIZE       3       /* 3'rd long  size of init'ed data */
  48. #define HDR_BSIZE       4       /* 4'th long  size of bss */
  49. #define HDR_TOTMEM      6       /* 6'th long  total memory */
  50.  
  51. #define HDR_LEN         8       /* total length of header */
  52.  
  53. /* Miscellaneous definitions */
  54. #define STRLEN          4       /* default minimum string length */
  55. #define STRBUF          512     /* buffer length for strings */
  56.  
  57. int main(int argc, char **argv);
  58. void strings(char *filename);
  59. void usage(void);
  60. int match_accent(int c);
  61.  
  62. int strmin = STRLEN;            /* minimum string length */
  63. int printoff = 0;               /* print octal offset of each str */
  64. int objall = 0;                 /* search entire a.out file, not */
  65. int acceptacc = 0;              /* accept french accents */
  66.  
  67. static unsigned char accents[]={0xE9,0xE0,0xE7,0xE8,0xEA,0xF9,0};
  68.  
  69. int match_accent(int c)
  70. {
  71.   int i,rc=0;
  72.  
  73.   if (acceptacc)
  74.   {
  75.    i=0;
  76.    while ((accents[i]!=0)&&(!rc))
  77.    {
  78.      rc= (c==accents[i++]);
  79.    }
  80.  
  81.   }
  82.   return rc;
  83.  
  84. }
  85.  
  86. /* Just initialized data segment */
  87.  
  88. int main(argc, argv)
  89. /* [<][>][^][v][top][bottom][index][help] */
  90. int argc;
  91. char *argv[];
  92. {
  93.   while ((++argv, --argc) && '-' == (*argv)[0]) {
  94.         if (!strcmp(*argv, "-"))
  95.                 ++objall;
  96.         else if (!strcmp(*argv, "-o"))
  97.                 ++printoff;
  98.         else if (!strcmp(*argv, "-a"))
  99.                 ++acceptacc;
  100.         else if (isdigit((*argv)[1]))
  101.                 strmin = atoi(&(*argv)[1]);
  102.         else
  103.                 usage();
  104.   }
  105.  
  106.   if (0 == argc) usage();
  107.   while (argc--) strings(*argv++);
  108.   return(0);
  109. }
  110.  
  111. void strings(filename)
  112. /* [<][>][^][v][top][bottom][index][help] */
  113. char *filename;
  114. {
  115.   char buf[STRBUF];             /* the strings buffer */
  116.   char *bufptr;                 /* pointer into the strings buffer */
  117.   FILE *input;                  /* input file */
  118.   long header[HDR_LEN];         /* buffer for reading the header */
  119.   long offset;                  /* file offset */
  120.   long limit;                   /* limit, if doing data segment only */
  121.   int c;                        /* input character */
  122.  
  123.   if (NULL == (input = fopen(filename, "r"))) {
  124.         fprintf(stderr, "strings: ");
  125.         perror(filename);
  126.         exit(1);
  127.   }
  128.   if (HDR_LEN == fread(header, sizeof(long), (size_t)HDR_LEN, input)
  129.       && (SMALLMAGIC == header[HDR_MAGIC]
  130.         ||SEPARATEMAGIC == header[HDR_MAGIC]) && !objall) {
  131.         offset = header[HDR_HSIZE] + header[HDR_TSIZE]; /* object file */
  132.         limit = offset + header[HDR_DSIZE];
  133.   } else {
  134.         offset = 0L;
  135.         limit = 0L;
  136.   }
  137.  
  138.   fseek(input, offset, 0);
  139.   bufptr = buf;
  140.  
  141.   while (!limit || offset < limit) {
  142.         if (EOF == (c = getc(input))) break;
  143.         if ((('\0' == c || '\n' == c) && bufptr - buf >= strmin)
  144.             || (bufptr - buf == STRBUF - 1)) {
  145.                 *bufptr = '\0';
  146.                 if (printoff) printf("%lo:", offset - (bufptr - buf));
  147.                 puts(buf);
  148.                 bufptr = buf;
  149.         } else if ((' ' <= c && c < 0177) || ('\t' == c) || (match_accent(c)))
  150.                 *bufptr++ = c;
  151.         else
  152.                 bufptr = buf;
  153.  
  154.         ++offset;
  155.   }
  156.  
  157.   fclose(input);
  158. }
  159.  
  160. void usage()
  161. {
  162.   fprintf(stderr, "usage: strings [-] [-o] [-a] [-len] file ...\n");
  163.   exit(1);
  164. }
  165.